/**
 * Copyright (c) www.longdw.com
 */
package com.jdy.haoduoaiteacher.lyric;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.util.CountDownTimerWithPause;
import com.jdy.haoduoaiteacher.widget.RoundProgressBar;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;
import java.util.List;

public class LyricAdapter extends BaseAdapter {
  private static final String TAG = LyricAdapter.class.getSimpleName();

  /** 歌词句子集合 */
  List<LyricSentence> mLyricSentences = null;

  Context mContext = null;

  // private boolean bShowProgress;
  // private boolean inCircleProgress;

  private final SparseArray<View> mSparseArray;

  private CountDownTimerWithPause mCountDownTimer;

  // public boolean isShowProgress() {
  // return bShowProgress;
  // }
  //
  // public void setShowProgress(boolean bShowProgress) {
  // this.bShowProgress = bShowProgress;
  // }

  /** 当前的句子索引号 */
  int mIndexOfCurrentSentence = 0;

  float mCurrentSize = 20;
  float mNotCurrentSize = 17;

  public LyricAdapter(Context context) {
    mContext = context;
    mLyricSentences = new ArrayList<LyricSentence>();
    mIndexOfCurrentSentence = 0;

    mSparseArray = new SparseArray<View>();
  }

  /** 设置歌词，由外部调用， */
  public void setLyric(List<LyricSentence> lyric) {
    mLyricSentences.clear();
    if (lyric != null) {
      mLyricSentences.addAll(lyric);
      Log.i(TAG, "歌词句子数目=" + mLyricSentences.size());
    }
    mIndexOfCurrentSentence = 0;
  }

  @Override
  public boolean isEmpty() {
    // 歌词为空时，让ListView显示EmptyView
    if (mLyricSentences == null) {
      Log.i(TAG, "isEmpty:null");
      return true;
    } else if (mLyricSentences.size() == 0) {
      Log.i(TAG, "isEmpty:size=0");
      return true;
    } else {
      Log.i(TAG, "isEmpty:not empty");
      return false;
    }
  }

  @Override
  public boolean isEnabled(int position) {
    // 禁止在列表条目上点击
    return false;
  }

  @Override
  public int getCount() {
    return mLyricSentences.size();
  }

  @Override
  public Object getItem(int position) {
    return mLyricSentences.get(position).getContentText();
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  int progress = 100;

  public int getProgress() {
    return progress;
  }

  public void setProgress(int progress) {
    this.progress = progress;
  }

  @SuppressLint("InflateParams")
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    convertView = mSparseArray.get(position);

    if (convertView == null) {
      holder = new ViewHolder();
      LayoutInflater inflater = (LayoutInflater) mContext
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = inflater.inflate(R.layout.lyric_line, null);
      holder.lyric_line = (TextView) convertView
          .findViewById(R.id.lyric_line_text);

      holder.roundProgressBar = (RoundProgressBar) convertView
          .findViewById(R.id.roundProgressBar);
      convertView.setTag(holder);
      mSparseArray.put(position, convertView);

      if (position == 1) {

        final long remainTime = mLyricSentences.get(1).getStartTime();
        final RoundProgressBar pb = holder.roundProgressBar;
        // final int step = (int) ((100 * 100) / mLyricSentences.get(1)
        // .getStartTime());

        progress = (int) (remainTime / 100);
        holder.roundProgressBar.setMax(progress);
        holder.roundProgressBar.setProgress(progress);
        holder.roundProgressBar.setVisibility(View.VISIBLE);

        mCountDownTimer = new CountDownTimerWithPause(remainTime, 100, true) {

          @Override
          public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            pb.setProgress(progress--);
          }

          @Override
          public void onFinish() {
            // TODO Auto-generated method stub
            pb.setVisibility(View.GONE);
          }

          @Override
          public void onCancel() {
            // TODO Auto-generated method stub
            pb.setVisibility(View.GONE);
          }
        };
      }

    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    if (position >= 0 && position < mLyricSentences.size()) {
      holder.lyric_line.setText(mLyricSentences.get(position).getContentText());
    }
    if (mIndexOfCurrentSentence == position) {
      // 当前播放到的句子设置为白色，字体大小更大
      holder.lyric_line.setTextColor(Color.RED);
      holder.lyric_line.setTextSize(mCurrentSize);
    } else {
      // 其他的句子设置为暗色，字体大小较小
      holder.lyric_line.setTextColor(mContext.getResources().getColor(
          R.color.textview_font_color));
      holder.lyric_line.setTextSize(mNotCurrentSize);
    }
    return convertView;
  }

  public void startCountDown() {
    if (mCountDownTimer != null)
      mCountDownTimer.create();
  }

  //
  // public void pauseCountDown() {
  // if (mCountDownTimer != null)
  // mCountDownTimer.pause();
  // }

  public void resumeCountDown() {
    if (mCountDownTimer != null)
      mCountDownTimer.resume();
  }

  public void abortCountDown() {
    if (mCountDownTimer != null)
      mCountDownTimer.abort();
  }

  public void setCurrentSentenceIndex(int index) {
    mIndexOfCurrentSentence = index;
  }

  static class ViewHolder {
    TextView lyric_line;
    RoundProgressBar roundProgressBar;
  }
}
